#ifndef _UIOBJECT_H_LULY_
#define _UIOBJECT_H_LULY_
// #include <map>
#include <string>

#include "../PLATFORM.h"
#include "../Rect.h"
#include "vector_map.h"
class UIObject {
  friend class _UI;

 public:
  // WHMode 布局长宽的设置方式
  enum class WHMode {
    FREE,  //自由，可以自己设置长宽
    FIT,   //适应内容，根据内容的大小设置长宽
    FILL   //填充，将长宽设置成最大（填充到父对象）
  };

 protected:
  vector_map<std::string, UIObject*> child_objects;
  UIObject* parent_object = nullptr;

 protected:
  std::string uiObjectId;
  bool enable = true;
  int x = 0;
  int y = 0;
  int contentX = 0;  //内容的坐标(是相对于本对象来说的)
  int contentY = 0;
  int relativeX = 0;  //相对于父对象的坐标//和x,y保持同步
  int relativeY = 0;
  int width = 0;
  int height = 0;
  int contentWidth = 0;  //内容的长宽
  int contentHeight = 0;
  double scale = 1;
  double angle = 0;
  WHMode widthMode = WHMode::FIT;
  WHMode heightMode = WHMode::FIT;
  float widthWeight = 1;  //权重，当mode为fill时处理比例
  float heightWeight = 1;

  virtual void init();
  virtual void event();
  virtual void update();
  virtual void updateContentData();
  virtual void draw();
  virtual void drawArea(const Rect& area);  //只绘制一部分

 public:
  UIObject(std::string uiObjectId, const int x = 0, const int y = 0,
           const int width = 0, const int height = 0, double scale = 1,
           double angle = 0);
  virtual void addChild(UIObject* child);
  UIObject* getChild(std::string uiObjectId);
  vector_map<std::string, UIObject*>& getChildMap() { return child_objects; }
  UIObject* getParent() { return parent_object; }
  std::string getId() { return uiObjectId; }

  //设置坐标
  virtual void setX(int x) { UIObject::x = x; }
  virtual void setY(int y) { UIObject::y = y; }
  virtual void setXY(int x, int y) {
    UIObject::x = x;
    UIObject::y = y;
  }
  int getX() { return x; }
  int getY() { return y; }
  void getXY(int& x, int& y) {
    x = UIObject::x;
    y = UIObject::y;
  }

  //内容区域
  int getContentX() { return contentX; }
  int getContentY() { return contentY; }
  void setContentXY(int x_r_t, int y_r_t) {
    contentX = x_r_t;
    contentY = y_r_t;
  }
  void setContentWidth(int width_c) { contentWidth = width_c; }
  void setContentHeight(int height_c) { contentHeight = height_c; }
  void setContentWH(int width_c, int height_c) {
    contentWidth = width_c;
    contentHeight = height_c;
  }
  int getContentWidth() { return contentWidth; }
  int getContentHeight() { return contentHeight; }

  //设置相对坐标
  void setRelativeX(int x_r) {
    UIObject::x =
        parent_object->getX() +
        x_r;  //为什么不直接relativeX=x呢？因为relativeX是根据x计算出来的，跟x同步。但x不跟relativeX同步
  }
  void setRelativeY(int y_r) { UIObject::y = parent_object->getY() + y_r; }
  void setRelativeXY(int x_r, int y_r) {
    UIObject::x = parent_object->getX() + x_r;
    UIObject::y = parent_object->getY() + y_r;
  }
  int getRelativeX() {
    return x - parent_object
                   ->getX();  //为什么还要计算呢？为了防止相对位置还没有同步
  }
  int getRelativeY() { return y - parent_object->getY(); }
  void getRelativeXY(int& x, int& y) {
    x = UIObject::x - parent_object->getX();
    y = UIObject::y - parent_object->getY();
  }

  //设置缩放大小
  virtual void setScale(double scale) { UIObject::scale = scale; }
  //获取大小
  double getScale() { return scale; }

  virtual void setWidth(int w) { width = w; }
  virtual void setHeight(int h) { height = h; }
  virtual int getWidth() { return width; }
  virtual int getHeight() { return height; }

  WHMode getWidthMode() { return widthMode; }
  WHMode getHeightMode() { return heightMode; }
  virtual void setWidthMode(WHMode wm) { widthMode = wm; }
  virtual void setHeightMode(WHMode hm) { heightMode = hm; }
  virtual void setWHMode(WHMode wm, WHMode hm) {
    setWidthMode(wm);
    setHeightMode(hm);
  }
  virtual void setWHMode(WHMode m) {
    widthMode = m;
    heightMode = m;
  }

  //获取权重
  float getWidthWeight() { return widthWeight; }
  float getHeightWeight() { return heightWeight; }

  //权重设置
  void setWidthWeight(float widthWeight) {
    UIObject::widthWeight = widthWeight;
  }
  void setHeightWeight(float heightWeight) {
    UIObject::heightWeight = heightWeight;
  }
  void setWeight(float widthWeight, float heightWeight) {
    UIObject::widthWeight = widthWeight;
    UIObject::heightWeight = heightWeight;
  }
  //宽高的权重都设为weight大小
  void setWeight(float weight) {
    UIObject::widthWeight = weight;
    UIObject::heightWeight = weight;
  }

  vector_map<std::string, UIObject*>& childObjects() { return child_objects; }

  virtual void setAngle(double angle) { UIObject::angle = angle; }

  //如果某点在该UIObject里面
  virtual bool isPointWithin(int pointX, int pointY) {
    if (pointX >= x && pointX <= x + width && pointY >= y &&
        pointY <= y + height) {
      return true;
    } else {
      return false;
    }
  }

  ~UIObject();
};

#endif